package com.example.voiceviewdemo;

import java.io.IOException;

import com.example.voiceviewdemo.RecodeManager.SoundAmplitudeListen;

import android.R.bool;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class RecordDialog {
	private Context context;
	private ImageView progress; // 显示录音的振幅
	private ImageView mic_icon;// 显示录音状态图标
	
	private boolean started = false;

	private Drawable[] progressImg = new Drawable[7];// 显示录音振幅图片缓存
	private ImageView btn_cancel;// 取消按钮
	private TextView btn_submit;
	private TextView txt_msg;// 显示计时器
	private TextView dialog_title;// 显示录音振幅

	private View dialog_view;// 录音对话视图
	private AlertDialog dialog;// 录音对话框

	private int countTime = 0;

	private Handler handler = new Handler();
	private boolean isSpeak = false;

	RecodeManager recodeManager = new RecodeManager();
	private Runnable run = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			txt_msg.setText(countTime + "秒");
			countTime++;
			handler.postDelayed(run, 1000);
		}

	};

	public RecordDialog(Context context) {
		this.context = context;
		dialog_view = LayoutInflater.from(context).inflate(R.layout.dialog_sound, null);

		// 初始化振幅图片
		progressImg[0] = context.getResources().getDrawable(R.drawable.mic_1);
		progressImg[1] = context.getResources().getDrawable(R.drawable.mic_2);
		progressImg[2] = context.getResources().getDrawable(R.drawable.mic_3);
		progressImg[3] = context.getResources().getDrawable(R.drawable.mic_4);
		progressImg[4] = context.getResources().getDrawable(R.drawable.mic_5);
		progressImg[5] = context.getResources().getDrawable(R.drawable.mic_6);
		progressImg[6] = context.getResources().getDrawable(R.drawable.mic_7);

		dialog = new AlertDialog.Builder(context).setView(dialog_view).show();
		// dialog.cancel();

		progress = (ImageView) dialog_view.findViewById(R.id.sound_progress);
		btn_cancel = (ImageView) dialog_view.findViewById(R.id.cancel);
		btn_submit = (TextView) dialog_view.findViewById(R.id.submit);
		mic_icon = (ImageView) dialog.findViewById(R.id.mic);
		dialog_title = (TextView) dialog.findViewById(R.id.title);
		txt_msg = (TextView) dialog.findViewById(R.id.msg);

		btn_cancel.setOnClickListener(onCancel);
		btn_submit.setOnClickListener(onSubmit);

	}

	private SoundAmplitudeListen onSoundAmplitudeListen = new SoundAmplitudeListen() {

		@SuppressWarnings("deprecation")
		@Override
		public void amplitude(int amplitude, int db, int value) {
			// TODO Auto-generated method stub
			if (value >= 6) {
				value = 6;
			}
			progress.setBackgroundDrawable(progressImg[value]);
			
			if(value > 3){
				if(!started){
					recodeManager.stopRecord();
					recodeManager.setSoundAmplitudeListen(onSoundAmplitudeListen);
					try {
						recodeManager.startRecordCreateFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				started = true;
			}else{
				started = false;
				recodeManager.stopRecord();
				try {
					recodeManager.startRecordCreateFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	};

	public void showDialog() {
		dialog.show();
	}

	private OnClickListener onCancel = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			dialog.cancel();
			dialog.dismiss();
		}

	};
	private OnClickListener onSubmit = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			// dialog_title.setText("请 稍等");
			if (!isSpeak) {
				handler.post(run);
				btn_submit.setText("完成");
				isSpeak = true;
				try {
					recodeManager.setSoundAmplitudeListen(onSoundAmplitudeListen);
					recodeManager.startRecordCreateFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				btn_submit.setText("开始");
				handler.removeCallbacks(run);
				recodeManager.stopRecord();
				isSpeak = false;
			}
		}
	};
}
